<?php 
namespace My\Controller;

use Utuo\BackgroundController;

class ProductActivityController extends BackgroundController {
	
	// 选择的商品id
	private $productGoodsId = null;
	
	// 所有活动
	private $activities = array(
		'1' => array('name' => "团购设置", 'action'=>'groupBuy', 'is_default' => 1),
	);
	
	public function _initialize() {
		$this->productGoodsId = I('product_goods_id', 0, "intval");
		$this->setControllerTitle("促销活动管理");
		foreach ($this->activities as $k=>$v) {
			$this->setActionTitle($v['action'], $v['name'], "product_goods_id={$this->productGoodsId}&type={$k}");
			if ( $v['is_default'] ) $this->setActionTitle("index", "团购设置", "product_goods_id={$this->productGoodsId}&type={$k}");
		}
		$this->setActionNav("groupBuy");
	}
	
	public function indexAction() {
		$type = I('type', 1, "intval");
		call_user_method($this->activities[$type]['action'].C("ACTION_SUFFIX"), $this);
	}
	
	// 团购设置
	public function groupBuyAction() {
		$this->assign("productActivity", $this->getActivity( 1 ));
		$this->display("groupBuy");
	}
	
	// 活动设置保存
	public function saveActivityAction() {
		false !== $this->submitPost("ProductActivity") and $this->ajaxSuccess("保存成功");
		$this->ajaxError("保存失败");
	}
	
	// 获得活动
	private function getActivity( $type ) {
		$productGoodsId = $this->productGoodsId; // 商品id
		$productActivity = M("ProductGoods a")->join("__PRODUCT_ACTIVITY__ b on a.id=b.product_goods_id and b.type={$type}", "LEFT")->where(array('a.id'=>$productGoodsId))->field("b.*,a.name as productGoodsName")->find();
		if ( $productActivity['id'] ) {
			empty($productActivity['ext_info']) or $productActivity['ext_info'] = unserialize( $productActivity['ext_info'] );
		} else {
			$productActivity['product_goods_id'] = $productGoodsId;
			$productActivity['type'] = $type;
		}
		$productActivity['isFinishedRadio'] = $this->getRadio('is_finished', array('0'=>'未结束', '1'=>'已结束'), $productActivity['is_finished']*1);
		return $productActivity;
	}
	
}
?>
